import Axios from "axios";
import qs from 'qs';

export async function verify() {
    // 从local storage里面获取token
    let token = localStorage.getItem("token");

    if (token === null) {
        return;
    }
    let resp = await Axios.get("/api/user/verify", {
        headers: {
            // 将token携带在headers信息里面传去后台
            authorization: `Bearer ${token}`,
        }
    });
    // 返回数据
    return resp.data;
}

export async function login(User) {
    let resp = await Axios.post("/api/user/login", qs.stringify(User));
    let token = resp.headers.authorization;
    if (token !== null || token !== "") {
        localStorage.setItem("token", token);
    }

    return resp.data;
}

export function logOut() {
    localStorage.removeItem("token");
}

export async function register(user) {
    let resp = await Axios.get('/api/user/register', {
        params: {
            method: 'register',
            userTel: user.userTel,
            password: user.password,
            username: user.username,
            time: user.date
        }
    });
    console.log(resp);
    return resp;
}

export async function getUsers() {
    let resp = await Axios.post('/api/user/findUsers');
    return resp;
}